#include "qcMonitorService.h"
#include "QC.h"
#include "env.h"
#include "QCPort.h"
#include "debug.h"

static QCHandler hQC;
static QCInitParms qcInitPrms =
    {
        .getDNVoltmv = QCPortDNGetVolt,
        .getDPVoltmv = QCPortDPGetVolt,
        .setDN = QCPortSetDN,
        .setDP = QCPortSetDP,
};

void sQCMonitorInit(void)
{
    hQC = QCInit(&qcInitPrms, QC_12V);
}

static unsigned int doOnce = 0;
void sQCMonitorLoop(void)
{
    if (env.chargerMode != E_CHARGE_MODE_QC)
    {
        return;
    }

    if (env.error == E_CRITICAL)
    {
        return;
    }
    
    QCLoop(hQC);
    if (isSupportQCProtocol(hQC) == QC_NOT_SUPPORT && doOnce == 0)
    {
        env.ready = E_SUPPLY_READY;
        doOnce = 1;
        PRINTF("[QC] not support!\n");
        env.chargerMode = E_CHARGE_MODE_NONE;
    }
    else if (isSupportQCProtocol(hQC) == QC_SUPPORT && doOnce == 0)
    {
        env.ready = E_SUPPLY_READY;
        doOnce = 1;
        PRINTF("[QC] support!\n");
        env.sourceCapVoltage = 12.0;
    }
}
